#!/bin/bash

. /lib/lsb/init-functions

RETVAL=0

start() {
	echo -n "Starting myvnc: "
	start-stop-daemon --start --quiet --chuid %USER% --exec /usr/local/bin/myvnc.sh
	RETVAL=$?
	log_end_msg $RETVAL
	echo
}

stop() {
	echo -n "Stopping myvnc: "
	start-stop-daemon --stop --name myvnc.sh --quiet --oknodo
	RETVAL=$?
	start-stop-daemon --stop --name Xvfb --quiet --oknodo
	[ $RETVAL -eq 0 ] && RETVAL=$?
	start-stop-daemon --stop --name x11vnc --quiet --oknodo
	[ $RETVAL -eq 0 ] && RETVAL=$?
	log_end_msg $RETVAL
}

restart() {
	stop
	start
}

case "$1" in
  start)
	start
	;;
  stop) 
	stop
	;;
  restart|force-reload|reload)
	restart
	;;
  status)
	status_of_proc /usr/bin/x11vnc myvnc && exit 0 || exit $?
	;;
  *)
	echo $"Usage: $0 {start|stop|status|restart|reload|force-reload}"
	exit 1
esac

exit $RETVAL
